﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.SoldierExam.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;

public partial class Aptitude_SearchAptitude : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (Request.QueryString["Where"] == null)
            {

                UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
                if (userInfo.DepartmentID.Substring(0, 4) == "1300")
                {
                    ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100')");
                }
                else
                {
                    ddlDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("(DepartmentID='" + userInfo.DepartmentID.Substring(0, 4) + "000') ");
                }

                ddlDepartment.DataTextField = "FullName";
                ddlDepartment.DataValueField = "DepartmentID";
                ddlDepartment.DataBind();
                ddlExam.DataTextField = "Subject";
                ddlExam.DataValueField = "ExamID";
                ddlExam.DataSource = AptitudeExam.getList();
                ddlExam.DataBind();

            }
            else
            {
                tb.Visible = false;
                BindGv();
            }
        }

    }

    protected void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        gvStandard.PageIndex = PageIndex;

        BindGv();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300309"))
        {
            this.ListPageContral1.SetFirstPage();
            this.gvStandard.PageIndex = 0;
            BindGv();
        }
        else
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
            return;
        }

    }

    protected void BindGv()
    {
        if (Request.QueryString["Where"] == null)
        {
            bool Contranct;
            if (rbtContract.SelectedValue == "0")
            {
                Contranct = false;
            }
            else
            {
                Contranct = true;
            }
            string where = "ExamID=" + ddlExam.SelectedValue;
            if (ddlStandard.SelectedValue != "")
            {
                where += "and Standard=" + ddlStandard.SelectedValue + " ";
            }
            List<AptitudeStandardInfo> list = AptitudeStandard.GetSearchStandard(ddlDepartment.SelectedValue, txtSoldierName.Text, Contranct, where);
            ListPageContral1.ItemCount = list.Count;

            gvStandard.DataSource = list;
            gvStandard.DataBind();
        }
        else
        {
            string Departmentid = Request.QueryString["Departmentid"];
            string Contranct = Request.QueryString["Contranct"];

            string Standard = Request.QueryString["Standard"];
            string Examid = Request.QueryString["Examid"];
            string RankerID = Request.QueryString["RankerID"];

            string where = " Examid=" + Examid;
            if (RankerID != "0")
            {
                where += " and RangerID= " + RankerID;
            }
            if (Standard != "-1")
            {
                where += where == "" ? "" : " And ";
                where += " Standard=" + Standard + " ";
            }

            List<AptitudeStandardInfo> list = AptitudeStandard.GetSearchStandard(Departmentid, Convert.ToInt32(Contranct), where);
            ListPageContral1.ItemCount = list.Count;

            gvStandard.DataSource = list;
            gvStandard.DataBind();
        }



    }



    public string GetFullDepartmentName(string SoldierID)
    {
        Soldier ss = new Soldier();
        SoldierArchiveInfo info = ss.GetSoldierInfoByID(SoldierID);

        return info.DepartmentName;
    }

    public string GetSoldierName(string soldierID)
    {

        Yipusoft.SoldierArchives.BLL.Soldier bll = new Yipusoft.SoldierArchives.BLL.Soldier();
        return bll.GetSoldierInfoByID(soldierID).SoldierName;
    }

    public string GetRangerName(int rangerID)
    {
        return AptitudeRanger.GetRangerById(rangerID).Ranger;
    }

    public string GetStandard(int standard)
    {
        string ret = "";
        switch (standard)
        {
            case -1:
                ret = "不合格";
                break;
            case 0:
                break;
            case 1:
                ret = "合格";
                break;
        }
        return ret;
    }
    protected void gvStandard_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = Convert.ToString(gvStandard.PageSize * ListPageContral1.CurrentPageIndex + (e.Row.RowIndex + 1));
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            //笔试成绩
            AptitudeStandardInfo info = e.Row.DataItem as AptitudeStandardInfo;
            if (info != null)
            {
                Label lblBiShi = e.Row.FindControl("lblBiShi") as Label;

                lblBiShi.Text = AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13) == null ? "" : AptitudeExamScore.GetAptitudeExamScore(info.SoldierID.ToString(), info.RangerID, info.ExamID, 13).Score.ToString("0.00");
            }
        }
    }
}
